﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins.AssetManagement;
using MethodWorx.CMS.Workflow;

namespace MethodWorx.CMS.TemplateParser.Installer.Components
{
    public class Assets : IInstallationWorkflowComponent
    {
        public Workflow.StepExecutionResult Execute(Workflow.IWorkflowExecutionContext context, InstallerWorkflowComponentConfiguration configuration)
        {
            using(var uow = context.CmsDataProvider.GetUnitOfWorkFactory().Begin())
            {
                //
                //  ok, here we go
                var service = new AssetManagementService(context.CmsDataProvider);
                var result = this.CreateFolderAndUploadAssets(service, service.GetRootAssetFolder(), configuration.ConfigurationElement, configuration);
                if (!result.StepCompleted)
                    return result;

                uow.Commit();
            }
            return Workflow.StepExecutionResult.Ok();
        }

        private Workflow.StepExecutionResult CreateFolderAndUploadAssets(AssetManagementService service, AssetFolder parent, TemplateElement element, InstallerWorkflowComponentConfiguration configuration)
        {
            foreach(var childElement in element.Elements)
            {
                //
                //  ok, get the anem
                var name = childElement.Name;
                if(name.ToLower() == "asset")
                {
                    //  ok, here we are creating an asset
                    var content = childElement.GetAttributeValue("content", "", true);
                    if (content != null)
                    {
                        if (!configuration.Files.ContainsKey(content.ToLower()))
                            return StepExecutionResult.Error("Installation File Not Found: " + content);

                        byte[] file = configuration.Files[content];
                        //
                        //  ok, we have the file contents
                        //  so lets sort it out
                        service.CreateAsset(parent, content, service.GetMimeType(content), file);
                    }
                    else
                        return StepExecutionResult.Error("No content for asset");

                }
                else
                {
                    //  otherwise here we want to create a folder if it does not exist
                    var found = parent.Children.FirstOrDefault(af => af.Name.ToLower() == name.ToLower());
                    if(found == null)
                        found = service.CreateAssetFolder(parent.Id, name, true);

                    var result = this.CreateFolderAndUploadAssets(service, found, childElement, configuration);
                    if (!result.StepCompleted)
                        return result;
                }
            }

            return StepExecutionResult.Ok();
        }
    }
}
